package com.zygk.library.base;


import android.os.Bundle;
import android.support.annotation.Nullable;

public abstract class LazyFragment extends BaseFragment {
    // 标志位，标志已经初始化完成。
    private boolean isPrepared;
    private boolean isVisible;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        init();
        isPrepared = true;
        onVisible();
    }
    /**
     * 在这里实现Fragment数据的缓加载.
     * @param isVisibleToUser
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if(getUserVisibleHint()) {
            isVisible = true;
            onVisible();
        } else {
            isVisible = false;
            onInvisible();
        }
    }
    private void onVisible(){
        if(isPrepared && isVisible){
            lazyLoad();
        }
    }
    protected abstract void lazyLoad();
    protected abstract void init();
    private void onInvisible(){}
}
